package com.ehome.poct.core.utils;

import com.ehome.exception.BaseBusiEnum;
import com.ehome.exception.BizException;

import java.util.Objects;

/**
 * @auther: kuiba.peng
 * @date: 2022/11/1 16:19
 * @description:
 */
public class EHomeAssert {

    public static void notNull(Object obj, BaseBusiEnum bizCode) {
        if (Objects.isNull(obj)) {
            throwException(bizCode);
        }
    }

    public static void notNull(Object obj, BaseBusiEnum bizCode, Object... message) {
        if (Objects.isNull(obj)) {
            throwException(bizCode, message);
        }
    }

    public static void notNull(Object obj, int bizCode, String message) {
        if (Objects.isNull(obj)) {
            throw new BizException(bizCode, message);
        }
    }

    public static void isTrue(boolean flag, BaseBusiEnum bizCode) {
        if (!flag) {
            throwException(bizCode);
        }
    }

    public static void isTrue(boolean flag, BaseBusiEnum bizCode, Object... message) {
        if (!flag) {
            throwException(bizCode, message);
        }
    }

    static void throwException(BaseBusiEnum bizCode) {
        throw new BizException(bizCode.getBusiCode(), bizCode.getMsg());
    }

    static void throwException(BaseBusiEnum bizCode, Object... args) {
        throw new BizException(bizCode.getBusiCode(), String.format(bizCode.getMsg(), args));
    }
}
